package top.mcwebsite.lib.route.api

import androidx.navigation.NavHostController
import java.lang.ref.WeakReference

object EasyRouter {

    private lateinit var navController: WeakReference<NavHostController>

    fun init(navHostController: NavHostController) {
        this.navController = WeakReference(navHostController)
    }

    fun navigate(path: String, builder: (RouterRequestBuilder.() -> Unit)? = null) {
        val request = RouterRequestBuilder(path)
            .apply { if (builder != null) this.builder() }
            .build()
        navController.get()?.navigate(request)
    }

    fun popBackStack() {
        navController.get()?.popBackStack()
    }
}
